package com.liukai.bishe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 所有fragment的父fragment
 */
public abstract class BaseFragment extends Fragment {

    private Unbinder mBind;

    /**
     * 创建布局
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = loadRootView(inflater,container);
        initDate();
        getFragmentContext(view);
        initView(view);
        initAdapter(view);
        initListener();
//        initAdapter(view);

        return view;
    }

    /**
     * 初始化数据
     */
    protected void initDate() {

    }

    protected abstract Context getFragmentContext(View view);

    /**
     * 设置适配器
     */
    protected void initAdapter(View view){

    }

    /**
     * 设置监听
     */
    protected void initListener(){

    }

    /**
     * 绑定组件
     * @param view
     */
    protected void initView(View view) {
        mBind = ButterKnife.bind(this, view);
    }

    /**
     * 返回一个布局
     * @param inflater
     * @param container
     * @return
     */
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(getLayoutId(),container,false);
    }

    /**
     * 获取布局的ID
     * @return
     */
    protected abstract int getLayoutId();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mBind != null) {
            mBind = null;
        }
        release();
    }

    protected void release() {

    }
}
